package com.wowudi.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wowudi.model.power.User;
import com.wowudi.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.json.JacksonJsonParser;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping("/admin/user/consumer")
public class UserController {

    @Autowired
    private UserService userService;

    /*
     * 登录
     * */
    @RequestMapping("login")
    public Map<String, Object> login(@RequestBody String data){
        Map<String, Object> objectMap = new JacksonJsonParser().parseMap(data);
        HashMap<String, Object> hashMap = userService.login(String.valueOf(objectMap.get("username")), String.valueOf(objectMap.get("password")));
        HashMap<String, Object> map= new HashMap<>();

        if (((User)hashMap.get("user")) != null){
            map.put("token",((User)hashMap.get("user")).getUAccount()+"-token");
            hashMap.put("code",20000);
            hashMap.put("data",map);
        }
        System.out.println("登录成功！！！！！！");
        return hashMap;
    }

    /*
     * 登录
     * */
    @GetMapping("getInfo")
    public Map<String, Object> getInfo(String token){
        System.out.println(token);
        Map<String, Object> objectMap = new HashMap<>();
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("name",token);
        objectMap.put("code",20000);
        objectMap.put("messge","成功");
        objectMap.put("data",hashMap);
        return objectMap;
    }


    /*
     * 查询所有员工
     * */
    @RequestMapping("findList")
    public HashMap<String,Object> findList(Integer page,Integer limit){
        Page<User> userPage = userService.findList(page, limit);
        HashMap<String, Object> hashMap = new HashMap<>();
        HashMap<String, Object> map = new HashMap<>();

        map.put("items",userPage.getRecords());
        map.put("total",userPage.getTotal());

        hashMap.put("code",20000);
        hashMap.put("data",map);

        return hashMap;
    }


    /*
     * 添加员工
     * */
    @RequestMapping("add")
    public HashMap<String,Object> add(User user){
        int i = userService.add(user);
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("code",20000);
        hashMap.put("msg",i==1?"添加成功":"系统繁忙");
        return hashMap;
    }



    /*
     * 修改员工
     * */
    @RequestMapping("update")
    public HashMap<String,Object> update(User user){
        int i = userService.update(user);
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("code",20000);
        hashMap.put("msg",i==1?"修改成功":"系统繁忙");
        return hashMap;
    }


    /*
     * 删除员工
     * */
    @RequestMapping("delete")
    public HashMap<String,Object> delete(int id){
        int i = userService.deleteById(id);
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("code",20000);
        hashMap.put("msg",i==1?"删除成功":"系统繁忙");
        return hashMap;
    }
}
